<?php
/**
 * 设置中心
 * author itholiday@126.com
 */
global $_W, $_GPC;
load()->func('tpl');
$html_title = $_W['current_module']['title'];
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'list';
$extra = !empty($_GPC['extra']) ? $_GPC['extra'] : '';
if ($op == 'tuiguang') {
    if (is_post() && $extra == 'upload') {
        $file = $_FILES["file"];
        if (!$file) {
            ajaxError('请上传图片');
        }
        $allowedExts = array("jpeg", "jpg", "png");
        $temp = explode("/", $file["type"]);
        $extension = end($temp);

        if (!in_array($extension, $allowedExts)) {
            ajaxError('只能上传图片文件');
        }
        if ($file["size"] > 4 * 1024 * 1024) {
            ajaxError('图片大小超过4M');
        }
        $file_dir = '../attachment/common/image/' . date('Ymd');//web文件夹的上层即为根目录

        if (!file_exists($file_dir)) {
            mkdirs($file_dir, '757');
        }
        $filename = $file_dir . '/' . md5(time() . uniqid()) . '.' . $extension;
        $res = move_uploaded_file($file["tmp_name"], $filename);
        if (!$res) {
            @unlink($filename);
            ajaxError('上传失败，稍后重试');
        }
        $imgurl = substr($filename, 14);
        if ($file['size'] > 2 * 1024 * 1024) {
            $new_path = $file_dir . '/' . md5(time() . uniqid()) . '.' . $extension;
            $res = image_png_size_add($filename, $new_path);
            if (!$res) {
                @unlink($filename);
                ajaxError('上传失败');
            }
            $imgurl = substr($new_path, 14);
        }
        ajaxSuccess('上传成功', cmf_get_upload_url($imgurl));
    }

    if (is_ajax() && $extra == 'save') {
        $options = array(
            'default' => '/addons/yq_haircircle/qrcode-background.png',
            'thumb1' => cmf_asset_relative_url($_GPC['thumb1']),
        );
        cmf_set_option('tuiguang_setting', $options);
        ajaxSuccess('保存成功');
    }
    $setting = cmf_get_option('tuiguang_setting');
    include $this->template('web/setting_tuiguang');
    exit;
}
//站点设置
if ($op == 'site') {
    if (is_ajax()) {
        $options['site_name'] = trim($_GPC['site_name']);
        $options['site_kefu1'] = trim($_GPC['site_kefu1']);
        $options['site_kefu2'] = trim($_GPC['site_kefu2']);
        $options['site_kefu3'] = trim($_GPC['site_kefu3']);
        cmf_set_option('site_setting', $options);
        ajaxSuccess('保存成功');
    }
    $setting = cmf_get_option('site_setting');
    include $this->template('web/setting_site');
    exit;
}

//首页轮播
if ($op == 'slider') {
    if (is_post() && $extra == 'upload') {
        $file = $_FILES["file"];
        if (!$file) {
            ajaxError('请上传图片');
        }
        $allowedExts = array("jpeg", "jpg", "png");
        $temp = explode("/", $file["type"]);
        $extension = end($temp);

        if (!in_array($extension, $allowedExts)) {
            ajaxError('只能上传图片文件');
        }
        if ($file["size"] > 4 * 1024 * 1024) {
            ajaxError('图片大小超过4M');
        }
        $file_dir = '../attachment/slider/' . date('Ymd');//web文件夹的上层即为根目录
        if (!file_exists($file_dir)) {
            mkdirs($file_dir, '757');
        }
        $filename = $file_dir . '/' . md5(time() . uniqid()) . '.' . $extension;
        $res = move_uploaded_file($file["tmp_name"], $filename);
        if (!$res) {
            @unlink($filename);
            ajaxError('上传失败，稍后重试');
        }
        $insertData['imgurl'] = substr($filename, 14);
        if ($file['size'] > 2 * 1024 * 1024) {
            $new_path = $file_dir . '/' . md5(time() . uniqid()) . '.' . $extension;
            $res = image_png_size_add($filename, $new_path);
            if (!$res) {
                @unlink($filename);
                ajaxError('添加失败');
            }
            $insertData['imgurl'] = substr($new_path, 14);
        }
        $insertData['create_time'] = time();
        $res = pdo_insert('yq_haircircle_home_slider', $insertData);
        if (empty($res)) {
            @unlink($file_dir . '/' . basename($insertData['imgurl']));
            ajaxError('添加失败');
        }
        ajaxSuccess('添加成功');
    }
    if (is_post() && $extra == 'ting' && isset($_GPC['yes'])) {
        if (isset($_GPC['id'])) {
            $id = $_GPC['id'];
            $yes = $_GPC['yes'] ? 1 : 0;
            $msg = $yes ? '启用' : '停用';
            if($yes){
                $res = pdo_fetchcolumn("select count(*) as Number from ims_yq_haircircle_home_slider where isopen = 1");
               if ($res>=4){
                   ajaxError('您已添加够四条,不能再添加了');
               }
            }
            $res = pdo_update('yq_haircircle_home_slider', ['isopen' => $yes], ['id' => $id]);
            if (!empty($res)) {
                ajaxSuccess($msg . '成功');
            }
            ajaxError($msg . '失败');
        }
        if (isset($_GPC['ids'])) {
            $ids = implode(',', $_GPC['ids']);
            $yes = $_GPC['yes'] ? 1 : 0;
            $msg = $yes ? '启用' : '停用';
            $sql = "update ims_yq_haircircle_home_slider set isopen=" . $yes . " where id in ($ids)";
            $res = pdo_query($sql);
            if (!empty($res)) {
                ajaxSuccess($msg . '成功');
            }
            ajaxError($msg . '失败');
        }
    }
    if (is_post() && $extra == 'del') {
        if (isset($_GPC['id'])) {
            $id = $_GPC['id'];
            $res = pdo_update('yq_haircircle_home_slider', ['delete_time' => time()], ['id' => $id]);
            if (!empty($res)) {
                ajaxSuccess('删除成功');
            }
            ajaxError('删除失败');
        }
        if (isset($_GPC['ids'])) {
            $ids = implode(',', $_GPC['ids']);
            $sql = "update ims_yq_haircircle_home_slider set delete_time=" . time() . " where id in ($ids)";
            $res = pdo_query($sql);
            if (!empty($res)) {
                ajaxSuccess('删除成功');
            }
            ajaxError('删除失败');
        }
    }
    $pagesize = 15;
    $page = max(1, $_GPC['page']);
    $start = ($page - 1) * $pagesize;

    $where = 'id>0 ';

    $start_time = isset($_GPC['start_time']) && !empty($_GPC['start_time']) ? strtotime($_GPC['start_time']) : '';
    $end_time = isset($_GPC['end_time']) && !empty($_GPC['end_time']) ? strtotime($_GPC['end_time']) : '';
    if (!empty($start_time) && !empty($end_time)) {
        $where .= " and create_time>=$start_time and create_time<=$end_time ";
    } else {
        if (!empty($start_time)) {
            $where .= " and create_time>=$start_time ";
        }
        if (!empty($end_time)) {
            $where .= " and create_time<=$end_time ";
        }
    }
    $total = pdo_fetchcolumn('SELECT COUNT(*) AS `number` FROM ' . tablename('yq_haircircle_home_slider') . ' WHERE ' . $where);
    //生成分页HTML
    $pager = getPageHTML($total, $page, $pagesize);
    $sql = "select * from ims_yq_haircircle_home_slider where $where and delete_time=0 order by isopen desc,id desc limit $start,$pagesize";
    $list = pdo_fetchall($sql);
    $list = empty($list) ? array() : $list;
    include $this->template('web/setting_slider');
    exit;
}

//任务参数
if ($op == 'task_param') {
    if (is_ajax()) {
        $options['tx_min'] = $_GPC['tx_min'] ? floatval($_GPC['tx_min']) : 0;
        $options['tx_fee'] = $_GPC['tx_fee'] ? floatval($_GPC['tx_fee']) : 0;
        $options['task_receive_max'] = $_GPC['task_receive_max'] ? intval($_GPC['task_receive_max']) : 0;
        $options['renqi_base'] = $_GPC['renqi_base'] ? intval($_GPC['renqi_base']) : 0;
        $options['fabu_base'] = $_GPC['fabu_base'] ? intval($_GPC['fabu_base']) : 0;
        $options['wancheng_base'] = $_GPC['wancheng_base'] ? intval($_GPC['wancheng_base']) : 0;
        $options['share_task_profit'] = $_GPC['share_task_profit'] ? floatval($_GPC['task_receive_max']) : 0;
        cmf_set_option('task_param_setting', $options);
        ajaxSuccess('保存成功');
    }
    $setting = cmf_get_option('task_param_setting');
    include $this->template('web/setting_task_param');
    exit;
}

//介绍文本
if ($op == 'introduce') {
    if (is_ajax()) {
        $options['project_introduce'] = trim($_GPC['project_introduce']);
        $options['about_us'] = trim($_GPC['about_us']);
        $options['new_introduce'] = trim($_GPC['new_introduce']);
        $options['cooperate'] = trim($_GPC['cooperate']);
        cmf_set_option('introduce_setting', $options);
        ajaxSuccess('保存成功');
    }
    $setting = cmf_get_option('introduce_setting');
    include $this->template('web/setting_introduce');
    exit;
}

if ($op == 'share') {
    if (is_post() && $extra == 'upload') {
        $file = $_FILES["file"];
        if (!$file) {
            ajaxError('请上传图片');
        }
        $allowedExts = array("jpeg", "jpg", "png");
        $temp = explode("/", $file["type"]);
        $extension = end($temp);

        if (!in_array($extension, $allowedExts)) {
            ajaxError('只能上传图片文件');
        }
        if ($file["size"] > 4 * 1024 * 1024) {
            ajaxError('图片大小超过4M');
        }
        $file_dir = '../attachment/common/image/' . date('Ymd');//web文件夹的上层即为根目录
        if (!file_exists($file_dir)) {
            mkdirs($file_dir, '757');
        }
        $filename = $file_dir . '/' . md5(time() . uniqid()) . '.' . $extension;
        $res = move_uploaded_file($file["tmp_name"], $filename);
        if (!$res) {
            @unlink($filename);
            ajaxError('上传失败，稍后重试');
        }
        $imgurl = substr($filename, 14);
        if ($file['size'] > 2 * 1024 * 1024) {
            $new_path = $file_dir . '/' . md5(time() . uniqid()) . '.' . $extension;
            $res = image_png_size_add($filename, $new_path);
            if (!$res) {
                @unlink($filename);
                ajaxError('上传失败');
            }
            $imgurl = substr($new_path, 14);
        }
        ajaxSuccess('上传成功', cmf_get_upload_url($imgurl));
    }

    if (is_ajax() && $extra == 'save') {
        $options = array(
            'task_money' => floatval($_GPC['task_money']),
            'detail' => trim($_GPC['detail']),//要求
            'share_title' => trim($_GPC['share_title']),
            'share_content' => trim($_GPC['share_content']),
            'thumb1' => cmf_asset_relative_url($_GPC['thumb1']),
            'thumb2' => cmf_asset_relative_url($_GPC['thumb2']),
        );
        cmf_set_option('share_setting', $options);
        ajaxSuccess('保存成功');
    }
    $setting = cmf_get_option('share_setting');
    include $this->template('web/setting_share');
    exit;
}
if($op == 'pay'){
    if (is_ajax() && $extra == 'jsweixinpay') {
        $options['jsweixinpay']['mchid'] = trim($_GPC['mchid']);
        $options['jsweixinpay']['appid'] = trim($_GPC['appid']);
        $options['jsweixinpay']['appkey'] = trim($_GPC['appkey']);
        $options['jsweixinpay']['apikey'] = trim($_GPC['apikey']);
        cmf_set_option('pay_setting', $options);
        ajaxSuccess('保存成功');
    }
    $setting = cmf_get_option('pay_setting');
    include $this->template('web/setting_pay');
    exit;
}
if ($op == 'fenxiao') {
    if (is_ajax()) {
        $options['fenxiao_switch'] = intval($_GPC['fenxiao_switch']);
        $options['vip_upgrade_price'] = $_GPC['vip_upgrade_price'] ? floatval($_GPC['vip_upgrade_price']) : 0;
        $options['vip_upgrade_first_per'] = $_GPC['vip_upgrade_first_per'] ? floatval($_GPC['vip_upgrade_first_per']) : 0;
        $options['vip_task_first_per'] = $_GPC['vip_task_first_per'] ? floatval($_GPC['vip_task_first_per']) : 0;
        cmf_set_option('fenxiao_setting_v1.0', $options);
        ajaxSuccess('保存成功');
    }
    $setting = cmf_get_option('fenxiao_setting_v1.0');
    include $this->template('web/setting_fenxiao');
    exit;
}

//分销参数
//if ($op == 'fenxiao') {
//    if (is_ajax()) {
//        $options['vip_task_first_per'] = $_GPC['vip_task_first_per'] ? floatval($_GPC['vip_task_first_per']) : 0;
//        $options['agent_task_first_per'] = $_GPC['agent_task_first_per'] ? floatval($_GPC['agent_task_first_per']) : 0;
//        $options['agent_task_second_per'] = $_GPC['agent_task_second_per'] ? floatval($_GPC['agent_task_second_per']) : 0;
//        cmf_set_option('fenxiao_setting', $options);
//        ajaxSuccess('保存成功');
//    }
//    $setting = cmf_get_option('fenxiao_setting');
//    include $this->template('web/setting_fenxiao');
//    exit;
//}